package com.example.icedcap.topeka_fake.model.quiz;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import com.example.icedcap.topeka_fake.helper.ParcelableHelper;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Author: doushuqi
 * Date: 15/7/15
 * Time: 上午10:09
 * Email: shuqi.dou@singuloid.com
 */
public abstract class Quiz<A> implements Parcelable {
    private static final String TAG = "Quiz";
    private final String mQuestion;
    private final String mQuizType;
    private A mAnswer;
    private boolean mSolved;

    public Quiz(String question, A answer, boolean solved) {
        mQuestion = question;
        mAnswer = answer;
        mQuizType = getQuizType().getJsonName();
        mSolved = solved;
    }


    protected Quiz(Parcel in) {
        mQuestion = in.readString();
        mQuizType = in.readString();
        mSolved = ParcelableHelper.readBoolean(in);
    }

    public static final Creator<Quiz> CREATOR = new Creator<Quiz>() {
        @Override
        public Quiz createFromParcel(Parcel in) {
            int ordinal = in.readInt();
            QuizType quizType = QuizType.values()[ordinal];
            try {
                Constructor<? extends Quiz> constructor = quizType.getType().getConstructor(Parcel.class);
                return constructor.newInstance();
            } catch (InstantiationException | IllegalAccessException |
                    InvocationTargetException | NoSuchMethodException e) {
                Log.e(TAG, "createFromParcel ", e);
            }
            throw new UnsupportedOperationException("Could not create Quiz");
        }

        @Override
        public Quiz[] newArray(int size) {
            return new Quiz[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        ParcelableHelper.writeEnumValue(dest, getQuizType());
        dest.writeString(mQuestion);
        ParcelableHelper.writeBoolean(dest, mSolved);
    }

    public int getId() {
        return getQuestion().hashCode();
    }

    public abstract QuizType getQuizType();

    public abstract String getStringAnswer();

    public boolean isAnswerCorrect(A answer) {
        return mAnswer.equals(answer);
    }

    public String getQuestion() {
        return mQuestion;
    }

    public A getAnswer() {
        return mAnswer;
    }

    public void setAnswer(A answer) {
        mAnswer = answer;
    }

    public boolean isSolved() {
        return mSolved;
    }

    public void setSolved(boolean solved) {
        mSolved = solved;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Quiz)) {
            return false;
        }
        Quiz quiz = (Quiz) o;
        if (!mQuestion.equals(quiz.getQuestion())) {
            return false;
        }
        if (!mQuizType.equals(quiz.getQuizType())) {
            return false;
        }
        if (!mAnswer.equals(quiz.getAnswer())) {
            return false;
        }
        if (mSolved != quiz.mSolved) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = mQuestion.hashCode();
        result = 31 * result + mQuizType.hashCode();
        result = 31 * result + mAnswer.hashCode();
        result = 31 * result + (mSolved ? 1 : 0);
        return result;
    }

    @Override
    public String toString() {
        return getQuizType() + ": \"" + getQuestion() + "\"";
    }

}
